package com.fs.jzzn.pojo;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import lombok.Data;
import lombok.EqualsAndHashCode;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Formula;

import com.fs.base.param.SysConstant;
import com.fs.base.pojo.PEntity;
import com.fs.base.utils.ExcelUtils;

/**
 * @author TiddlerCJ
 * @desc 合同实体
 */
@Entity
@Table(name="p_contract")
@Data@EqualsAndHashCode(callSuper=false)
public class Contract extends PEntity{
	private String contractNo;			//建设单位合同编号-订单编号
	private String contractTitle; 		//建设单位订单标题
	
	private String outContractNo;	//合同编号 :唯一性，编制规则 H+区域首字母简拼+年份+索引
	
	private String contractName;	//合同名称
	private String signDate;		//签订日期
	
	private String partA;			//合同甲方
	private String partB;			//合同乙方
	private String partAuser;		//甲方经办人
	private String partBuser;		//乙方经办人
	private String partAphone;		//甲方联系电话
	private String partBphone;		//乙方联系电话
	private String department;		//所属项目部
	
	
	
	@Formula("(SELECT  SUM(p.budget_money) FROM p_project p WHERE p.contract_no=contract_no and p.contract_no <> '' )")
	private Double budgetMoney;		//预算金额
	
	@Formula("(SELECT SUM(p.actual_money) FROM p_project p WHERE p.contract_no=contract_no  and p.contract_no <> '')")
	private Double actualMoney;		//实际金额
	
	@Formula("(SELECT SUM(i.money) FROM p_invoice i WHERE i.p_no=contract_no)")
	private Double billMoney;		//开票金额
	
	private Double auditMoney;	//审计金额(此金额为填写)
	
	@Formula("(SELECT SUM(i.money) FROM p_income i WHERE i.p_no IN( SELECT p.p_no FROM p_project p WHERE p.contract_no = contract_no   and p.contract_no <> '' ))")
	private Double returnedMoney;	//回款金额
	
	private String payDesc;			//付款情况说明
	private String contractType;	//合同类别	自营/挂靠/分包
	
	private String mainUnit;		//总包单位

	private String assistUnit;		//分包单位
	
	private String contractLevel;				//合同级别  框架协议-合同订单
	
	@Transient
	private String signYear;		//签订年份
	public String getSignYear(){
		if(StringUtils.isNotBlank(this.signDate) && this.signDate.length()>=4){
			return this.signDate.substring(0,4);
		}
		return "未知";
	}
	
	public Contract() {
		super();
	}

	public static List<Contract> excelDataFormat(List<List<String>> data) {
		List<Contract> list = new ArrayList<Contract>();
		for(List<String> rowData: data){
			list.add(excelData2Bean(rowData));
		}
		//删除标题行
		//if(list.size()>0) { list.remove(0); }
		return list;
	}
	
	//"[\"订单编号\",\"合同编号\",\"合同类型\",\"合同名称\",\"签订日期\",\"合同甲方\",\"合同乙方\",\"甲方经办人\",\"甲方联系电话\",\"乙方经办人\",\"乙方联系电话\",\"预算金额\",\"实际金额\"
	//,\"开票金额\",\"审计金额\",\"回款金额\",\"付款情况及说明\",\"总包单位\",\"分包单位\"]"
	private static Contract excelData2Bean(List<String> row){
		row = ExcelUtils.dataLenFomart(row, 20);
		DecimalFormat df  = new DecimalFormat(".00");	
		Contract c = new Contract();
			c.setOutContractNo(row.get(0));//内部合同编号0
			c.setContractNo(row.get(1));//订单编号1
			c.setContractName(row.get(2));//订单名称2
			c.setContractType(row.get(3));//订单类型3
			c.setContractType(row.get(4));//级别4
			//年份5
			c.setSignDate(row.get(6));//签订日期6
			c.setPartA(row.get(7));//合同甲方7
			c.setPartB(row.get(8));//合同乙方8
			c.setPartAuser(row.get(9));//甲方经办人9
			c.setPartAphone(row.get(10));//甲方联系电话10
			c.setPartBuser(row.get(11));//乙方经办人11
			c.setPartBphone(row.get(12));//乙方联系电话12
			//预算金额13
			//实际金额14
			//开票金额15
			try {
				Double auditMoney = Double.parseDouble(row.get(16));
				c.setAuditMoney( Double.parseDouble(df.format(auditMoney)));
			} catch (Exception e) {
				System.err.println("审计金额格式有误："+row.get(16) );
			}
			//回款金额17
			c.setPayDesc(row.get(18));//付款情况及说明18
			c.setMainUnit(row.get(19));//总包单位19
			c.setAssistUnit(row.get(20));//分包单位20
			c.setOrgNo(row.get(21));//所属机构21
			c.setRemark(row.get(22));//备注22
			c.setCreateTime(new Date());//创建时间23
			//创建人24
			String userName = row.get(24);
			if(StringUtils.isNotBlank(userName)){
				String userId = SysConstant.getKey(SysConstant.USERS_ID_NAME_MAP, userName);
				c.setCreaterId(userId);
			}
			
			/*c.setContractName(row.get(4));//合同名称
			c.setSignDate(row.get(5));//签订日期
			c.setPartA(row.get(6));//合同甲方
			c.setPartB(row.get(7));//合同乙方
			c.setPartAuser(row.get(8));//甲方经办人
			c.setPartAphone(row.get(9));//甲方联系电话
			c.setPartBuser(row.get(10));//乙方经办人
			c.setPartBphone(row.get(11));//乙方联系电话
			//预算金额
			//实际金额
			//开票金额
			//审计金额
			try {
				Double auditMoney = Double.parseDouble(row.get(15));
				c.setAuditMoney( Double.parseDouble(df.format(auditMoney)));
			} catch (Exception e) {
				System.err.println("审计金额格式有误："+row.get(15) );
			}
			//回款金额
			c.setPayDesc(row.get(17));//付款情况
			c.setMainUnit(row.get(18));//总包单位
			c.setAssistUnit(row.get(19));//分包单位
			 */			
		return c;
	}
}
